#region License
// CommentController.cs
// 
// Copyright (c) 2012 Xoqal.com
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
// http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#endregion

namespace Kaqaz.Web.UI.Controllers
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using Xoqal.Data;
    using Xoqal.Web.Mvc.Security;

    public class CommentController : DataDrivenController
    {
        private readonly Data.ICommentRepository commentRepository;
        private readonly Data.IPostRepository postRepository;

        /// <summary>
        /// Initializes a new instance of the <see cref="CommentController" /> class.
        /// </summary>
        /// <param name="unitOfWork">The unit of work.</param>
        public CommentController(
            IUnitOfWork unitOfWork, 
            Data.ICommentRepository commentRepository, 
            Data.IPostRepository postRepository)
            : base(unitOfWork)
        {
            this.commentRepository = commentRepository;
            this.postRepository = postRepository;
        }

        /// <summary>
        /// POST: /Comment/Create
        /// </summary>
        /// <param name="comment">The comment.</param>
        /// <returns></returns>
        [HttpPost]
        public PartialViewResult Create(Entities.Comment comment)
        {
            try
            {
                comment.CreateTime = DateTime.Now;
                comment.IsAuthorComment = Xoqal.Security.Authentication.Default.IsUserInPermission(Common.Security.Permissions.CommentManagement);

                this.commentRepository.Add(comment);

                var post = this.postRepository.GetPost(comment.PostId);
                ViewBag.NeedsApprove = post.NeedCommentApprove;

                return this.PartialView("CommentResult", comment);
            }
            catch
            {
                ViewBag.HasError = true;
                return this.PartialView("CommentResult");
            }
        }

        /// <summary>
        /// GET: /Comment/Unapproved
        /// </summary>
        /// <returns></returns>
        [Permission("CommentManagement")]
        public ActionResult Unapproved()
        {
            return this.View(this.commentRepository.Query.Where(c => !c.IsApproved && !c.IsAuthorComment && c.Post.NeedCommentApprove));
        }

        /// <summary>
        /// GET: /Comment/Delete/1
        /// </summary>
        /// <param name="id">The id.</param>
        /// <returns></returns>
        [Permission("CommentManagement")]
        [HttpGet]
        public ActionResult Delete(long id)
        {
            this.commentRepository.Delete(c => c.Id == id);
            return this.RedirectToAction("Unapproved");
        }

        /// <summary>
        /// GET: /Comment/Approve/1
        /// </summary>
        /// <param name="id">The id.</param>
        /// <returns></returns>
        [Permission("CommentManagement")]
        public ActionResult Approve(long id)
        {
            var comment = this.commentRepository.GetComment(id);
            if (comment == null)
            {
                return new HttpNotFoundResult();
            }

            comment.IsApproved = true;
            return this.RedirectToAction("Unapproved");
        }
    }
}
